package com.group02.community.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.user.SysPost;
import com.group02.community.mapper.PostMapper;
import com.group02.community.service.IPostService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, SysPost> implements IPostService {
    @Resource
    private PostMapper sysPostMapper;

    @Override
    public IPage<SysPost> getPage(Long current, Long size) {
        System.out.println(1111);
        IPage<SysPost> page = sysPostMapper.getPage(new Page<SysPost>(current, size));
        System.out.println(page);
        return page;
    }

    @Override
    public List<SysPost> getQueryPage(String postCode, String postName, String status) {
        return sysPostMapper.getPost(postCode, postName, status);
    }

    @Override
    public void iaDelete(String postId) {
        sysPostMapper.iaDelete(postId);
    }

    @Override
    public void deleteGroup(List<String> ids) {
        sysPostMapper.deleteGroup(ids);
    }

    @Override
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<SysPost> sysPosts = sysPostMapper.selectList(null);
        for (int i = 0; i < sysPosts.size(); i++) {
            SysPost sysPost = sysPosts.get(i);
            if(sysPost.getStatus().equals("0")){
                sysPost.setStatus("正常");
            }else{
                sysPost.setStatus("停用");
            }
        }
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlsx");
        EasyExcel.write(response.getOutputStream(),SysPost.class).sheet(System.currentTimeMillis()+"").doWrite(sysPosts);
    }

    @Override
    public Long getUserByPostId(String id) {
        return sysPostMapper.getUserByPostId(id);
    }

}
